package controllers

import (
	"github.com/astaxie/beego"
	_ "github.com/go-sql-driver/mysql"
	"html/template"
	"src/gitee.com/wx_e6924dbcd0/notebook/models"
	"strconv"
)

type MainController struct {
	beego.Controller
}

func (c *MainController) Get() {

	c.TplName = "userbook.html"

	// 提交数据 //http://127.0.0.1:8080/main/addmessage 处理

}

//http://127.0.0.1:8080/main/getmessagelist?offset=0&limit=2
func (c *MainController) GetMessageList() {

	// 读取数据库内容
	// 引用model,->orm
	// orm -> 引用 conf 配置连接DB
	// orm 读取数据库

	result := make(map[string]interface{})
	result["count"] = 0

	//获取 offset 转成int 类型
	offsetStr := template.HTMLEscapeString(c.Input().Get("offset"))
	offset, err := strconv.Atoi(offsetStr)
	if err != nil {
		result["msg"] = "get dataList failed 1" + err.Error()
		c.Data["json"] = result
		c.ServeJSON()
		return

	}

	//获取limit 转成int 类型
	limitStr := template.HTMLEscapeString(c.Input().Get("limit"))
	limit, err := strconv.Atoi(limitStr)
	if err != nil {
		result["msg"] = "get dataList failed 2" + err.Error()
		c.Data["json"] = result
		c.ServeJSON()
		return
	}

	notebookList, err := models.GetNoteList(offset, limit)
	if err != nil {
		result["msg"] = "get dataList failed" + err.Error()
		c.Data["json"] = result
		c.ServeJSON()

		//失败返回数据
		return
	}

	var count = models.GetNoteCount() //获取用户总数

	result["count"] = count
	result["data"] = notebookList

	c.Data["json"] = result
	c.ServeJSON()
}

//http://127.0.0.1:8080/main/getonemessage
func (c *MainController) GetOneMessage() {

	idStr := template.HTMLEscapeString(c.Input().Get("id"))
	id, err := strconv.Atoi(idStr)
	if err != nil {
		c.Data["json"] = err.Error()
		c.ServeJSON()
		return
	}

	notebook := models.GetNoteById(int64(id))
	//println(notebook)

	// 读取数据库内容
	// 引用model,->orm
	// orm -> 引用 conf 配置连接DB
	// orm 读取数据库
	//fmt.Println("123")
	c.Data["json"] = notebook
	c.ServeJSON()

}

//http://127.0.0.1:8080/main/addmessage?name=mo&passwd=99&sex=%E5%A5%B3&content=qwqwq
func (c *MainController) AddMessage() {

	// 引用model,->orm
	// orm -> 引用 conf 配置连接DB
	// orm 插入数据到数据库

	name := template.HTMLEscapeString(c.Input().Get("name"))
	sex := template.HTMLEscapeString(c.Input().Get("sex"))
	passwd := template.HTMLEscapeString(c.Input().Get("passwd"))
	content := template.HTMLEscapeString(c.Input().Get("content"))

	if name != "" && passwd != "" && sex != "" && content != "" {

		// orm insert

		id, err := models.NoteAdd(name, sex, passwd, content)

		result := make(map[string]interface{})
		result["id"] = id
		if err != nil {
			result["msg"] = "AddMessage failed" + err.Error()
		} else {
			result["msg"] = "OK"
		}

		c.Data["json"] = result

		c.ServeJSON()
		return

	}
	//c.ServeJSON()
	c.TplName = "userbook.html"
	//
	return

	// 返回json 数据， 页面 通过js location 跳到 评论列表 //http://127.0.0.1:8080/main/getmessagelist

}

//http://127.0.0.1:8080/main/delnotebyid
func (c *MainController) DelNoteById() {

	// 读取数据库内容
	// 引用model,->orm
	// orm -> 引用 conf 配置连接DB
	// orm 读取数据库

	idStr := template.HTMLEscapeString(c.Input().Get("id"))
	id, err := strconv.Atoi(idStr)
	if err != nil {
		c.Data["json"] = err.Error()
		c.ServeJSON()
	}

	result := make(map[string]interface{})

	//执行删除操作
	delNubmer, err := models.DelNoteById(int64(id))
	if err != nil {

		result["count"] = 0
		result["msg"] = "del failed" + err.Error()
		c.Data["json"] = result
		c.ServeJSON()

		//失败返回数据
		return
	}

	// 成功返回数据
	result["count"] = delNubmer
	result["msg"] = "ok"

	c.Data["json"] = result
	c.ServeJSON()
}

//http://127.0.0.1:8080/main/upmessage?id=4&name=hh&sex=%E5%A5%B3&passwd=126&content=jjppo
func (c *MainController) UpMessage() {

	// 读取修改数据库内容
	// 引用model,->orm
	// orm -> 引用 conf 配置连接DB
	// orm 读取修改数据库

	idStr := template.HTMLEscapeString(c.Input().Get("id"))
	id, err := strconv.Atoi(idStr)
	if err != nil {
		c.Data["json"] = err.Error()
		c.ServeJSON()
	}

	name := template.HTMLEscapeString(c.Input().Get("name"))
	sex := template.HTMLEscapeString(c.Input().Get("sex"))
	passwd := template.HTMLEscapeString(c.Input().Get("passwd"))
	content := template.HTMLEscapeString(c.Input().Get("content"))
	result := make(map[string]interface{})
	if name != "" && passwd != "" && sex != "" && content != "" {

		// orm insert

		Upuser := models.UpMessage(int64(id), name, sex, passwd, content)

		result["user"] = Upuser
		//if err != nil {
		//	result["msg"] = "UpMessage failed" + err.Error()
		//} else {
		//	result["msg"] = "OK"
		//}

		c.Data["json"] = result

		//c.ServeJSON()
		//return

	}

	// 成功返回数据

	result["msg"] = "ok"

	c.Data["json"] = result
	c.ServeJSON()
}
